// user/meInfoEdit/meInfoEdit.js
import http from '../../http'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		top: '',
		left: '',
		height: '',
		width: '',

		type: '',
		info: {
			nickname: '',
			username: '',
		},
	},
	// 返回
	back() {
		wx.navigateBack({
			delta: 1
		});
	},
	inputChange(event) {
		console.log(event);
		let info = this.data.info
		info[this.data.type] = event.detail
		this.setData({
			info: info
		})
	},
	confirm() {
		let data = {
			id: this.data.info.id
		}
		if (this.data.type == 'nickname') {
			data.nickname = this.data.info.nickname
		} else if (this.data.type == 'username') {
			data.username = this.data.info.username
		} else if (this.data.type == 'mobile') {
			data.mobile = this.data.info.mobile
		} else if (this.data.type == 'email') {
			data.email = this.data.info.email
		}
		http.put({
			url: '/system/user/profile/update',
			data: data,
			then: (res) => {
				console.log('修改资料', res);
				if (res.data.code == 0) {
					wx.showToast({
						title: '修改成功',
						icon: 'success',
					})
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.setData({
			top: wx.getStorageSync('customTop'),
			left: wx.getStorageSync('screenWidth') - wx.getStorageSync('customRight'),
			height: wx.getStorageSync('customHeight'),
		})
		let pages = getCurrentPages()
		let currentPage = pages[pages.length - 1]
		let options = currentPage.options
		this.setData({
			type: options.type
		})
		http.get({
			url: '/system/user/profile/get',
			then: (res) => {
				console.log('个人信息', res);
				let info = res.data.data
				if (info.posts) {
					let posts = info.posts.map(item => {
						return item.name
					})
					info.posts = posts.join()
				}
				if (info.roles) {
					let roles = info.roles.map(item => {
						return item.name
					})
					info.roles = roles.join()
				}
				this.setData({
					info: info,
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})